﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotSpatial.Controls;
using DotSpatial.Controls.Header;
using DotSpatial.Data;
using DotSpatial.Topology;
using System.Data;
using DotSpatial.Symbology;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
using System.Reflection;

namespace TerrainViewerPluginDotSpatial
{
    public class Plugin3d : Extension
    {
        private bool _3dButtonClicked = false;

        private Class3d _mainClass = new Class3d();
        
        public override void Activate() {

            //Directory for storing temporary files: snapshot grid, snapshot images and xml file.
            string tempDir = Path.Combine(Path.GetTempPath(), "mw3DViewer");

            if (!Directory.Exists(tempDir))
            {
                try
                {
                    Directory.CreateDirectory(tempDir);
                }
                catch
                {
                    tempDir = Path.GetTempPath();
                }
            }

            //these are the names of the required temporary files. These must be set as properties
            //of the main class
            _mainClass.SnapshotImageName1 = tempDir + Path.DirectorySeparatorChar + "snapshot.bmp";
            _mainClass.SnapshotImageName2 = tempDir + Path.DirectorySeparatorChar + "background.bmp";
            _mainClass.XmlFileName = tempDir + Path.DirectorySeparatorChar + "maps.xml";
            _mainClass.SnapshotRasterName = tempDir + Path.DirectorySeparatorChar + "snapshot.bgd";
            _mainClass.BilFileName = tempDir + Path.DirectorySeparatorChar + "snapshot.bil";
            
            //set the location of the TerrainViewer.exe
            string processDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            _mainClass.TerrainViewerExeFile = Path.Combine(processDir, "terrainViewer.exe");

            //set the reference to the main map
            _mainClass.MainMap = (Map)App.Map;

            try
            {
                //To add the 3d view button to the main toolbar
                SimpleActionItem mainButton = new SimpleActionItem(HeaderControl.HomeRootItemKey, "3d-view", MainButton_Click)
                {
                    GroupCaption = "Map Tool",
                    SmallImage = Properties.Resources._3D,
                    LargeImage = Properties.Resources._3D,
                    ToolTipText = "3D View - click on the map to show a 3D view of the surrounding area"
                };

                App.HeaderControl.Add(mainButton);
            }
            catch
            {
                MessageBox.Show("There was an error adding the 3d-view button.");
                return;
            }

            Map mm = (Map)App.Map;
            mm.MouseDown += new MouseEventHandler(Map_MouseDown);

            base.Activate();
        }

        /// <summary>
        /// When the plugin is deactivated
        /// </summary>
        public override void Deactivate() {
            App.HeaderControl.RemoveAll();
            _mainClass.RemoveSnapshotBoundary();
            Map map = (Map)App.Map;
            map.MouseDown -= Map_MouseDown;
            base.Deactivate();
        }

        /// <summary>
        /// When the '3d view' button on the main toolbar is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainButton_Click(object sender, EventArgs e) {
            if (!_3dButtonClicked)
            {
                _3dButtonClicked = true;
                App.Map.Cursor = Cursors.Cross;
            }
            else
            {
                _3dButtonClicked = false;
                App.Map.Cursor = Cursors.Arrow;
            }
        }

        /// <summary>
        /// When the 3d-button is active and the user clicks on the map
        /// </summary>
        /// <param name="sender">the map object</param>
        /// <param name="e">mouse event</param>
        public void Map_MouseDown(object sender, MouseEventArgs e) {
            if (!_3dButtonClicked) return;

            _mainClass.Run3dView(e.X, e.Y);
        }
    }
}
